#include "lib_config.h"
#include "at.h"
#include "string_operation.h"

char execute_cmd_line[MAX_EXECUTE_CMD_LINE_LENGTH] = {0};
char execute_cmd_name[MAX_EXECUTE_CMD_LENGTH] = {0};
char execute_cmd_parm[MAX_EXECUTE_PARAM_LENGTH] = {0};

void at_h();

void at_print() {
    LOG("AT+PRINT:%s\r\n", execute_cmd_parm);
}

void set() {
    LOG("%s\r\n", execute_cmd_parm);
}

AT_CMD cmd_list[MAX_CMD_LIST_LENGTH] = {
        {"H",     (void (*)()) at_h,         "get at help"},
        {"PRINT", (void (*)()) at_print,   "print what you sent!"},
        {"SET",   (void (*)()) set,          "set some thing"},
};

void at_h() {
    int index = 0;
    for (; index < MAX_CMD_LIST_LENGTH; index++) {
        if (cmd_list[index].function != NULL) {
            LOG("AT+%s:%s\r\n", cmd_list[index].cmd, cmd_list[index].help);
        } else {
            break;
        }
    }
}

int verify_solve_cmd_and_get_index(char *cmd) {
    int cmd_index = -1;
    int cmd_start;
    int cmd_end;
    int name_index = 0;

    int param_index;
    int param_start = -1;
    int param_end = -1;

    //验证参数正确
    if (has_symbols_by_index("AT", cmd, 0)) {

        if (has_symbols_by_index("+", cmd, 2)) {
            cmd_start = string_find_symbol_index('+', cmd) + 1;
            if (has_symbol('=', cmd)) {//提取参数及命令索引位置
                cmd_end = string_find_symbol_index('=', cmd);
                param_start = cmd_end + 1;
                param_end = string_length(cmd);
            } else {//提取命令名称
                cmd_end = string_length(cmd);
            }

            //提取命令
            for (; cmd[cmd_start] != '\0' && cmd_start < cmd_end; cmd_start++, name_index++) {
                execute_cmd_name[name_index] = cmd[cmd_start];
            }
            execute_cmd_name[name_index] = '\0';
            //提取参数
            for (param_index = param_start, name_index = 0;
                 cmd[param_index] != '\0' && param_index < param_end; param_index++, name_index++) {
                execute_cmd_parm[name_index] = cmd[param_index];
            }
            execute_cmd_parm[name_index] = '\0';

            //寻找可执行的命令
            for (name_index = 0; name_index < MAX_CMD_LIST_LENGTH; name_index++) {
                if (cmd_list[name_index].function != NULL) {
                    //LOG("CMD:%s\r\n",cmd_list[name_index].cmd);
                    if (string_case_equal(execute_cmd_name, cmd_list[name_index].cmd)) {
                        cmd_index = name_index;
                        break;
                    }
                } else {
                    break;
                }

            }

        }
    }
    return cmd_index;
}

int at_execute_cmd(char *cmd) {
    int cmd_index = verify_solve_cmd_and_get_index(cmd);
    LOG("CMD:%s,CMD_Index:%d\r\n", execute_cmd_name, cmd_index);
    LOG("CMD_Parm:%s\r\n", execute_cmd_parm);
    //执行命令
    if (cmd_index >= 0) {
        ((void *(*)()) (cmd_list[cmd_index].function))();
    } else {
        LOG("Error:%s is not define!\r\n", cmd);
        return EXECUTE_CMD_ERR;
    }
    return EXECUTE_OK;
}

int at_execute_cmds(char *cmds) {
    int counter = 0;
    while (*cmds != '\0') {
        if (*cmds == '\r' && *(cmds + 1) != '\0' && *(cmds + 1) == '\n') {
            execute_cmd_line[counter] = '\0';
            cmds += 2;
            counter = 0;
            //执行命令
            LOG("\r\n-------------------START------------------\r\n");
            at_execute_cmd(execute_cmd_line);
            LOG("\r\n--------------------END-------------------\r\n");
        }
        //防止越界
        if (counter < MAX_EXECUTE_CMD_LINE_LENGTH) {
            execute_cmd_line[counter] = *cmds;
            counter++;
        }

        cmds++;
    }
    return EXECUTE_OK;
}





